//
//  XJShouCangGoodsItem.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/5/14.
//  Copyright © 2018年 Tzyang. All rights reserved.
//  收藏的商品item

#import "XJShouCangGoodsItem.h"

//Model
#import "XJShouCangGoodsModel.h"

@interface XJShouCangGoodsItem()
/**商品图片*/
@property(nonatomic, strong) UIImageView *goodsIV;
@property(nonatomic, strong) UILabel *goodsNameLb;// 商品名
@property(nonatomic, strong) UILabel *goodsPriceLb;// 价格
@property(nonatomic, strong) UILabel *goodsOriginPriceLb; // 原价(高价)

@property(nonatomic, strong) UIView *selectedView; // 和cell一样大的选中view
/**圆形选中按钮*/
@property(nonatomic, strong) UIButton *dotBtn;
@end

@implementation XJShouCangGoodsItem
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

#pragma mark - setupUI
- (void)setupUI{
    
//    // 加边框
//    self.layer.borderColor = [UIColor blackColor].CGColor;
//    self.layer.borderWidth = 1.0;
    
    self.goodsIV = [UIImageView new];
    //    self.goodsIV.backgroundColor = [UIColor redColor];
    self.goodsIV.layer.masksToBounds = YES;
    self.goodsIV.layer.cornerRadius = 5;
    [self.contentView addSubview:self.goodsIV];
    
    self.goodsNameLb = [UILabel new];
    //    self.goodsNameLb.text = @"商品商品商品商品商品商品商品商品商品商品商品商品商品商品商品商品商品商品商品商品";
    self.goodsNameLb.numberOfLines = 0;
//    self.goodsNameLb.backgroundColor = [UIColor blueColor];
    [self.contentView addSubview:self.goodsNameLb];
    
    self.goodsPriceLb = [UILabel new];
    //    self.goodsPriceLb.text = @"￥99";
    self.goodsPriceLb.textColor = [UIColor redColor];
    self.goodsPriceLb.textAlignment = NSTextAlignmentLeft;
    
    [self.contentView addSubview:self.goodsPriceLb];
    
    self.goodsOriginPriceLb = [UILabel new];
    //    self.goodsOriginPriceLb.text = @"￥130.00";
    self.goodsOriginPriceLb.font = [UIFont systemFontOfSize:13.0];
    self.goodsOriginPriceLb.textColor = [UIColor lightGrayColor];
    //    self.goodsOriginPriceLb.backgroundColor = [UIColor brownColor];
    self.goodsOriginPriceLb.textAlignment = NSTextAlignmentRight;
    [self.contentView addSubview:self.goodsOriginPriceLb];
    
    self.selectedView = [UIView new];
    self.selectedView.backgroundColor = [UIColor colorWithRed:110.0/255.0 green:110.0/255.0 blue:110.0/255.0 alpha:0.5];
    self.selectedView.hidden = YES; // 默认隐藏
    [self.contentView addSubview:self.selectedView];
    
    self.dotBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.dotBtn.hidden = YES; // 默认隐藏
    [self.contentView addSubview:self.dotBtn];
    //    [self.selectedView addSubview:self.dotBtn];
    
    [self.dotBtn setImage:[UIImage imageNamed:@"selected_off"] forState:(UIControlStateNormal)];
    [self.dotBtn setImage:[UIImage imageNamed:@"selected_on"] forState:(UIControlStateSelected)];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.goodsIV.sd_layout.topEqualToView(self.contentView).centerXEqualToView(self.contentView).widthIs(150).heightIs(150);
    
    self.goodsNameLb.sd_layout.topSpaceToView(self.goodsIV, 2).leftSpaceToView(self.contentView, 5).widthIs(150).heightIs(50);
    
    self.goodsPriceLb.sd_layout.topSpaceToView(self.goodsNameLb, 0).leftSpaceToView(self.contentView, 8).widthIs(100).heightIs(20);
    self.goodsOriginPriceLb.sd_layout.topSpaceToView(self.goodsNameLb, 0).rightSpaceToView(self.contentView, 8).widthIs(100).heightIs(20);
    
    self.selectedView.frame = self.contentView.frame;
    
    // 选中取消选中小圆点
    self.dotBtn.frame = CGRectMake(self.frame.size.width - 50 ,20 , 30, 30);
}

- (void)setModel:(XJShouCangGoodsModel *)model {
    _model = model;
    
    [self.goodsIV sd_setImageWithURL:[NSURL URLWithString:model.cover] placeholderImage:[UIImage imageNamed:@"placeholderImg"]];
    self.goodsPriceLb.text = [NSString stringWithFormat:@"￥%.2f",(float)[model.price integerValue]];
    self.goodsNameLb.text = model.name;
    self.goodsOriginPriceLb.text = [NSString stringWithFormat:@"￥%0.2f",(float)[model.market_price integerValue]];
    
    self.dotBtn.selected = model.isScgoosModelSelected; //✅
}

#pragma mark - 是否显示选中小圆点
//- (void)rb_showOrHideSelectedViewWithModel:(XJShouCangGoodsModel *)model editingMode:(BOOL)isEditing {
//    // 如果是编辑状态:显示圆点。
//    if (isEditing) {
//        self.dotBtn.hidden = NO;
//        self.dotBtn.selected = model.isSelected;
//    }else {
//        self.dotBtn.hidden = YES;
//        self.dotBtn.selected = NO;
//    }
//}

- (void)xjscgoodsItem_showOrHideSelectedBtnWithState:(BOOL)state {
    self.dotBtn.hidden = !state;
}


@end
